package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import com.cheyun.netsalev3.bean.DaoDianParam
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.YaoyueParam
import com.cheyun.netsalev3.bean.basedata.Member
import com.cheyun.netsalev3.repository.HomeTaskDaoDianPageRepository
import com.cheyun.netsalev3.repository.HomeTaskYaoyuePageRepository
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.CustomerStoreActivity
import com.cheyun.netsalev3.view.ReceptionActivity
import com.cheyun.netsalev3.widget.MyDialog
import com.jeremyliao.liveeventbus.LiveEventBus

/**
 * @author Michael
 * @date  2018/10/30 - 15:55.
 */
class HomeTaskViewModel(var homeTaskYaoyueRepository: HomeTaskYaoyuePageRepository, var homeTaskDaoDianRepository: HomeTaskDaoDianPageRepository) : ViewModel() {
    //邀约数据
    var yaoyuelist = homeTaskYaoyueRepository.loadAndroidData()
    //到店数据
    var daodianlist = homeTaskDaoDianRepository.loadAndroidData()
    val networkState1 = Transformations.switchMap(yaoyuelist) { homeTaskYaoyueRepository.networkState }!!
    val networkState2 = Transformations.switchMap(daodianlist) { homeTaskDaoDianRepository.networkState }!!

    val totlelist1 = Transformations.switchMap(yaoyuelist) { homeTaskYaoyueRepository.total }!!
    val totlelist2 = Transformations.switchMap(daodianlist) { homeTaskDaoDianRepository.total }!!

    var loginData = MySharedPreferences.getLoginData()

    var memberlist = ArrayList<Member>()

    init {
        MySharedPreferences.getBaseData()?.member?.apply {
            memberlist.addAll(this)
        }
    }

    fun onClickScan(view: View){
        LiveEventBus.get().with("scan").postValue("")
    }
    //点击邀约列表
    fun onClickYaoyue(view: View, yaoyueParam: YaoyueParam) {
//        var intent = Intent(view.context, ReceptionActivity::class.java)
//        intent.putExtra("id", yaoyueParam.id.toString() )
//        view.context.startActivity(intent)
        loginData?.channel?.settings?.receive?.apply {
            if(this == 1){
                Toast.makeText(view.context,"无展督模式下不能操作",Toast.LENGTH_LONG).show()
            }else{
                if (yaoyueParam.is_arrive==1){
                    Toast.makeText(view.context,"已到店",Toast.LENGTH_LONG).show()
                }else{
                    var intent = Intent(view.context, ReceptionActivity::class.java)
                    intent.putExtra("id", yaoyueParam.id.toString() )
                    intent.putExtra("storeitemsname", yaoyueParam.storeitemsname)
                    intent.putExtra("tracktagname", yaoyueParam.tracktagname)
                    intent.putExtra("seriesname", yaoyueParam.seriesname)
                    view.context.startActivity(intent)
                }
            }
        }
    }

    //点击到店列表
    fun onClickDaoDian(view: View, daoDianParam: DaoDianParam) {
        loginData?.channel?.settings?.receive?.apply {
            if(this == 1){
                Toast.makeText(view.context,"无展督模式下不能操作",Toast.LENGTH_LONG).show()
            }else{
                var intent = Intent(view.context, CustomerStoreActivity::class.java)
                intent.putExtra("id", daoDianParam.id)
                view.context.startActivity(intent)
            }
        }
    }

    /**
     * 刷新
     */
    fun refresh(pos: Int) {
        when (pos) {
            1 -> homeTaskYaoyueRepository.dataSourceFactory.sourceLiveData.value?.invalidate()
            2 -> homeTaskDaoDianRepository.dataSourceFactory.sourceLiveData.value?.invalidate()
        }
    }

    /**
     * 分享
     */
    fun shareView(view: View, item: DaoDianParam) {
        var content = view.context

        if (content is AppCompatActivity) {
            //深拷贝
            var list = ArrayList<DialogParam>()
            for (it in memberlist) {
                var items = DialogParam(it.realname, it.uid.toString())
                items.ikey = item.id.toString()
                list.add(items)
            }
            val newFragment = MyDialog.newInstance("接待分享给", list)
            newFragment.huidia = this::myPopSetData
            newFragment.show(content.supportFragmentManager, "missiles")
        }
    }
    fun myPopSetData(item: DialogParam?) {
        item?.apply {
            homeTaskDaoDianRepository.shareData(this.ikey, this.id)
        }
    }
}